﻿namespace HuaWei
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Xml;

    public class XMLCommon
    {
        private static void botbmnerR(IDictionary idictionary, string str, XmlNode xmlNode)
        {
            ArrayList list2;
            if (xmlNode is XmlElement)
            {
                XmlNode node = xmlNode;
                if (node.Attributes.Count == 0)
                {
                    XmlNodeList childNodes = node.ChildNodes;
                    if (childNodes.Count == 0)
                    {
                        xmlNode = null;
                    }
                    else if ((childNodes.Count == 1) && (childNodes[0] is XmlText))
                    {
                        xmlNode = (XmlNode) childNodes[0];//
                    }
                }
            }
            object obj2 = idictionary[str];
            if (obj2 == null)
            {
                list2 = new ArrayList();
                idictionary[str] = list2;
            }
            else
            {
                list2 = (ArrayList) obj2;
            }
            list2.Add(xmlNode);
        }

        public static string CDataSetToXml(DataSet ds)
        {
            string str;
            if (ds == null)
            {
                return "";
            }
            if (ds.Tables.Count == 0)
            {
                return "";
            }
            MemoryStream w = null;
            XmlTextWriter writer = null;
            try
            {
                w = new MemoryStream();
                writer = new XmlTextWriter(w, Encoding.Default);
                ds.WriteXml(writer);
                int length = (int) w.Length;
                byte[] buffer = new byte[length];
                w.Seek(0L, SeekOrigin.Begin);
                w.Read(buffer, 0, length);
                str = Encoding.Default.GetString(buffer).Trim();
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    w.Close();
                    w.Dispose();
                }
            }
            return str;
        }

        public static DataSet CXmlToDataSet(string xmlStr)
        {
            if (!string.IsNullOrEmpty(xmlStr) && (xmlStr.Trim().Length != 0))
            {
                StringReader input = null;
                XmlTextReader reader = null;
                try
                {
                    DataSet set = new DataSet();
                    input = new StringReader(xmlStr);
                    reader = new XmlTextReader(input);
                    set.ReadXml(reader);
                    return set;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                        input.Close();
                        input.Dispose();
                    }
                }
            }
            return null;
        }

        public static string DataSetToJSON(DataSet ds)
        {
            return smethod(CDataSetToXml(ds));
        }

        public static DataSet JSONToDataSet(string jsonString)
        {
            return CXmlToDataSet(JsonToXml(jsonString));
        }

        public static string JsonToXml(string jsonString)
        {
            if ((jsonString != null) && (string.Empty != jsonString))
            {
                return string.Empty;
            }
            return string.Empty;
        }

        public static string smethod(string xml)
        {
            if ((xml != null) && (string.Empty != xml))
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(xml);
                try
                {
                    StringBuilder strBuilder = new StringBuilder();
                    strBuilder.Append("{ ");
                    smethod1(strBuilder, document.DocumentElement, true);
                    strBuilder.Append("}");
                    return strBuilder.ToString().Replace(@"<\>", "<>");
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }

        private static void smethod1(StringBuilder strBuilder, XmlNode xmlNode, bool flag)
        {
            if (flag)
            {
                strBuilder.Append(xmlNode.Name + ": ");
            }
            if ((xmlNode.Attributes != null) && (xmlNode.InnerXml.Length > 0))
            {
                strBuilder.Append("{");
                SortedList idictionary = new SortedList();
                foreach (XmlAttribute attribute in xmlNode.Attributes)
                {
                    botbmnerR(idictionary, attribute.Name, (XmlNode) attribute);//
                }
                foreach (XmlNode node in xmlNode.ChildNodes)
                {
                    if (node is XmlText)
                    {
                        botbmnerR(idictionary, "value", (XmlNode) node);//
                    }
                    else if (node is XmlElement)
                    {
                        botbmnerR(idictionary, node.Name, node);
                    }
                }
                foreach (string str in idictionary.Keys)
                {
                    ArrayList list2 = (ArrayList) idictionary[str];
                    if (list2.Count == 1)
                    {
                        smethod2(str, list2[0], strBuilder, true);
                    }
                    else
                    {
                        strBuilder.Append(str + ": [ ");
                        foreach (object obj2 in list2)
                        {
                            smethod2(str, obj2, strBuilder, false);
                        }
                        strBuilder.Remove(strBuilder.Length - 2, 2);
                        strBuilder.Append(" ], ");
                    }
                }
                strBuilder.Remove(strBuilder.Length - 2, 2);
                strBuilder.Append(" }");
            }
            else
            {
                strBuilder.Append("{}");
            }
        }

        private static void smethod2(string str, object obj, StringBuilder strBuilder, bool flag)
        {
            if (obj == null)
            {
                if (flag)
                {
                    strBuilder.Append(smethod3(str) + ": ");
                }
                strBuilder.Append("null");
            }
            else if (obj is string)
            {
                if (flag)
                {
                    strBuilder.Append(smethod3(str) + ": ");
                }
                string str2 = (string) obj;
                str2 = str2.Trim();
                strBuilder.Append(smethod3(str2));
            }
            else
            {
                smethod1(strBuilder, (XmlElement) obj, flag);
            }
            strBuilder.Append(", ");
        }

        private static string smethod3(string str)
        {
            if ((str == null) || (str.Length == 0))
            {
                return "\"\"";
            }
            int length = str.Length;
            StringBuilder builder = new StringBuilder(length + 4);
            builder.Append('"');
            for (int i = 0; i < length; i++)
            {
                char ch = str[i];
                switch (ch)
                {
                    case '\b':
                    {
                        builder.Append(@"\b");
                        continue;
                    }
                    case '\t':
                    {
                        builder.Append(@"\t");
                        continue;
                    }
                    case '\n':
                    {
                        builder.Append(@"\n");
                        continue;
                    }
                    case '\f':
                    {
                        builder.Append(@"\f");
                        continue;
                    }
                    case '\r':
                    {
                        builder.Append(@"\r");
                        continue;
                    }
                    case '"':
                    case '>':
                    case '\\':
                    {
                        builder.Append('\\');
                        builder.Append(ch);
                        continue;
                    }
                }
                if (ch < ' ')
                {
                    string s = new string(ch, 1);
                    string str4 = "000" + int.Parse(s, NumberStyles.HexNumber);
                    builder.Append(@"\u" + str4.Substring(str4.Length - 4));
                }
                else
                {
                    builder.Append(ch);
                }
            }
            builder.Append('"');
            return builder.ToString();
        }
    }
}

